//支付页
//author:王梦阳

var userId = localStorage.getItem("user"); //用户id

let getPrice = {
	version: "v1.0",
	getPriceInfo: function() {
		let data = eval('(' + localStorage.dataset + ')'); //获取商品data
		let btn_id = localStorage.id; //获取当前选择的商品属性id
		let obj = eval('(' + data.data[btn_id].item_description + ')'); //获取当前选择的商品属性值
		let item_num = localStorage.num; //获取当前选择的商品数量
		let htmlContent = ""; //装载下单商品
		let htmlContent1 = ""; //装载店铺
		let htmlContent2 = ""; //装载下单数量
		let htmlContent3 = ""; //装载红包
		let htmlContent4 = ""; //装载实付款
		let htmlContent5 = ""; //装载付款页红包
		let totalP = 0; //定义商品实付款
		let price = data.data[btn_id].item_price; //定义商品售价
		let item = JSON.stringify(data.data[btn_id]);
		let use_status = 0; //标志商品支付时是否使用红包
		htmlContent1 +=
			`<div>${data.data[btn_id].item_brand}旗舰店</div>`;
		$("#shop").html(htmlContent1);
		htmlContent +=
			`<div class="item" >
			<img class="item-image" src="${data.data[btn_id].item_img}"/>
			<div class="item-content">
			<span class="item-title">${data.data[btn_id].item_name}</span>
			<span class="attr-selected">${obj.value}x${item_num}</span>
			<span class="item-price">￥${data.data[btn_id].item_price}/件</span>
			</div>
		</div>`;
		$("#item-selected").html(htmlContent);

		//减少商品数量
		htmlContent2 +=
			`<span>${item_num}</span>`;
		$("#num").html(htmlContent2);
		$.ajax({
			url: "http://39.98.158.247:8040/coupon/check",
			data: {
				userId: userId
			},
			success: function(data) {
				if (data.data == null ||data.data=="used") {
					let htmlContent6 = "";
					htmlContent6 =
						`<div id="no-hongbao">您目前还没有可用的红包</div>`;
					$("#popover").html(htmlContent6);
					// 减少商品数量
					document.getElementById("lessen").addEventListener('tap', function() {
						htmlContent2 = "";
						if (item_num > 0) {
							item_num--;
							// console.log(num);
						}
						htmlContent2 +=
							`<span>${item_num}</span>`;
						$("#num").html(htmlContent2);
						//实付款
						totalP = price * item_num;
						htmlContent4 =
							`<span>￥${totalP}</span>`;
						$("#actual-price").html(htmlContent4);

					})
					// //增加商品数量
					document.getElementById("add").addEventListener('tap', function() {
						let htmlContent2 = "";
						item_num++;
						// console.log(num);
						htmlContent2 +=
							`<span>${item_num}</span>`;
						$("#num").html(htmlContent2);
						//实付款
						totalP = price * item_num;
						htmlContent4 =
							`<span>￥${totalP}</span>`;
						$("#actual-price").html(htmlContent4);

					})
					//实付款
					totalP = price * item_num;
					htmlContent4 =
						`<span>￥${totalP}</span>`;
					$("#actual-price").html(htmlContent4);
					// 点击立即支付事件
					document.getElementById("pay").addEventListener('tap', function() {
						console.log(item);
						if (userId != "") {
							if (use_status == 0) {
								$.ajax({
									type: "PUT",
									contentType: "application/json",
									url: "http://39.98.158.247:8040/item/buy?userId=" + userId + "&count=" + item_num,
									dataType: "json",
									data: item,
									success: function(data) {
										console.log(data);
										if (data.status == "success") {
											mui.toast("下单成功");
											window.location.href = "order-detail.html?order=" + data.data.order_id;
										} else {
											mui.toast("下单失败");
										}
									},
									error: function() {
										console.log("request error");
									}
								})
							} else {
								$.ajax({
									type: "PUT",
									contentType: "application/json",
									url: "http://39.98.158.247:8040/item/buy?userId=" + userId + "&count=" + item_num + "&couponId=" +
										couponId,
									dataType: "json",
									data: item,
									success: function(data) {
										console.log(data);
										if (data.status == "success") {
											mui.toast("下单成功");
											window.location.href = "order-detail.html?order=" + data.data.order_id;
										} else {
											mui.toast("下单失败");
										}
									},
									error: function() {
										console.log("request error");
									}
								})
							}
						} else {
							window.location.href = "login.html?npg=pay-item.html";
						}
					})
				} else {
					console.log(data);
					//判断红包是否过期
					let endTime = data.data.end_time;
					let date = new Date(endTime.replace(/-/g, '/')); //开始时间
					let time1 = date.getTime();
					let timestamp = (new Date()).getTime();
					let time = time1 - timestamp;
					let couponId = data.data.coupon_id;
					let discount = data.data.discount;
					console.log(time1);
					console.log(timestamp);
					console.log(time);
					if (time >= 0) {
						htmlContent3 +=
							`<div id="hong-detail">
						     <span id="hong-count">￥${data.data.discount}</span>
							 <span id="end-time">过期时间：${data.data.end_time}</span>
							 <span id="use">|立即使用</span>
						</div>
						<div id="no-use">不使用优惠</div>`;
						$("#popover").html(htmlContent3);
					} else {
						htmlContent3 +=
							`<div id="guoqi-hong-detail">
						     <span id="guoqi-hong-count">￥${data.data.discount}</span>
							 <span id="guoqi-end-time">红包已过期</span>
	
						</div>`;
						$("#popover").html(htmlContent3);
					}

					//减少商品数量
					document.getElementById("lessen").addEventListener('tap', function() {
						htmlContent2 = "";
						if (item_num > 0) {
							item_num--;
						}
						htmlContent2 +=
							`<span>${item_num}</span>`;
						$("#num").html(htmlContent2);
						//实付款
						if (use_status == 1 && item_num != 0) {
							totalP = price * item_num - discount;
							console.log(totalP);
						} else {
							totalP = price * item_num;
						}
						htmlContent4 =
							`<span>￥${totalP}</span>`;
						$("#actual-price").html(htmlContent4);

					})
					// //增加商品数量
					document.getElementById("add").addEventListener('tap', function() {
						let htmlContent2 = "";
						item_num++;
						// console.log(num);
						htmlContent2 +=
							`<span>${item_num}</span>`;
						$("#num").html(htmlContent2);
						//实付款
						if (use_status == 1 && item_num != 0 && time > 0) {
							totalP = price * item_num - discount;
						} else {
							totalP = price * item_num;
						}
						htmlContent4 =
							`<span>￥${totalP}</span>`;
						$("#actual-price").html(htmlContent4);

					})
					//实付款
					if (use_status == 1 && item_num != 0) {
						totalP = price * item_num - discount;
					} else {
						totalP = price * item_num;
					}

					htmlContent4 =
						`<span>￥${totalP}</span>`;
					$("#actual-price").html(htmlContent4);

					//点击使用红包后实付款改变
					document.getElementById("hong-detail").addEventListener('tap', function() {
						use_status = 1;
						console.log(use_status);
						//实付款
						if (use_status == 1 && item_num != 0) {
							totalP = price * item_num - discount;
						} else {
							totalP = price * item_num;
						}
						htmlContent4 =
							`<span>￥${totalP}</span>`;
						$("#actual-price").html(htmlContent4);
						mui.toast("红包使用成功");
					})

					//不使用优惠点击事件处理
					document.getElementById("no-use").addEventListener('tap', function() {
						use_status = 0;
						console.log(use_status);
						//实付款
						if (use_status == 1 && item_num != 0) {
							totalP = price * item_num - discount;
						} else {
							totalP = price * item_num;
						}
						htmlContent4 =
							`<span>￥${totalP}</span>`;
						$("#actual-price").html(htmlContent4);
						mui.toast("取消使用优惠");
					})

					//点击立即支付触发事件
					document.getElementById("pay").addEventListener('tap', function() {
						// console.log(item);
						if (userId != "") {
							if (use_status == 0) {
								$.ajax({
									type: "PUT",
									contentType: "application/json",
									url: "http://39.98.158.247:8040/item/buy?userId=" + userId + "&count=" + item_num,
									dataType: "json",
									data: item,
									success: function(data) {
										console.log(data);
										if (data.status == "success") {
											mui.toast("下单成功");
											window.location.href = "order-detail.html?order=" + data.data.order_id;
										} else {
											mui.toast("下单失败");
										}
									},
									error: function() {
										console.log("request error");
									}
								})
							} else {
								$.ajax({
									type: "PUT",
									contentType: "application/json",
									url: "http://39.98.158.247:8040/item/buy?userId=" + userId + "&count=" + item_num + "&couponId=" +
										couponId,
									dataType: "json",
									data: item,
									success: function(data) {
										console.log(data);
										if (data.status == "success") {
											mui.toast("下单成功");
											window.location.href = "order-detail.html?order=" + data.data.order_id;
										} else {
											mui.toast("下单失败");
										}
									},
									error: function() {
										console.log("request error");
									}
								})
							}
						} else {
							window.location.href = "login.html?npg=pay-item.html";
						}
					})
				}
			},
			error: function() {
				console.log("request error");
			}
		})
		//点击红包出现红包弹窗
		document.getElementById("hongbao").addEventListener('tap', function() {
			mui('.mui-popover').popover('toggle', document.getElementById("openPopover"));
		})
	}
}

window.onload = function() {
	getPrice.getPriceInfo();
}
